



import { CreateNode } from "./CreateNode";
import { Main } from "./Main";
import { MoveHexa } from "./MoveHexa";


const {ccclass, property} = cc._decorator;

@ccclass
export  class Map extends cc.Component {

    private _Mapfragments:cc.Node = null;

    private _MapfragmentsWidth:number = 0;

    private _MapfragmentsHeight:number = 0;

    /** 界面所有可防止六边形节点 用来显示可防止的位置，显示可用于放置的位置*/
    public _hexagonal_List:Array<cc.Node> = new Array();

    private _mapdata:Array<Array<number>> = new Array();

    private _lightenList:Array<cc.Node> = new Array();

    public countIndex:number = 0;

    

    get MapfragmentsWidth(){
        return this._MapfragmentsWidth;
    }

    onLoad () {

        this._Mapfragments = new CreateNode("Mapfragments");
        let spr:cc.SpriteFrame = cc.loader.getRes("main/60",cc.SpriteFrame);
        if(!spr){
            cc.warn("未获取到图片资源");
        }
        let map_sr =  this._Mapfragments.addComponent(cc.Sprite);
        map_sr.sizeMode = 1;
        this._Mapfragments.setContentSize(spr.getOriginalSize().width,spr.getOriginalSize().height);
        this._MapfragmentsWidth = this._Mapfragments.width;
        this._MapfragmentsHeight = this._Mapfragments.height;
        this.addMap();
        this.addLighten();

    }

    /** 添加地图 */
    private addMap (){
        cc.log("添加地图");
        //计算斜角距离
        let oblique_angle_x = this._MapfragmentsWidth;
        let oblique_angle_y = this._MapfragmentsWidth * Math.cos(30*Math.PI/180);
        let symbol = -1;
        let offset_x = 0;
        let offset_y = -1;
        let columns = [4,5,6,7,6,5,4];
        for(let i=0;i<columns.length;i++){
            this._mapdata[i] = [];
            if(i > columns.indexOf(7)){
                offset_y++;
            }
            for(let j=0;j<columns[i];j++){
                let mapNode:cc.Node = new cc.Node();
                mapNode.name = "Mapfragments";
                let spr:cc.SpriteFrame = cc.loader.getRes("main/60",cc.SpriteFrame);
                if(!spr){
                    cc.warn("未获取到图片资源");
                }
                mapNode.setContentSize(spr.getOriginalSize().width,spr.getOriginalSize().height);
                let map_sr =  mapNode.addComponent(cc.Sprite);
                map_sr.spriteFrame = spr;
                map_sr.sizeMode = 1;         
                this.node.addChild(mapNode);
                this._hexagonal_List.push(mapNode);
                if(i > columns.indexOf(7)){
                    symbol = 1;
                    mapNode.setPosition(-oblique_angle_x/2*5 + offset_x*oblique_angle_x + symbol * offset_y*oblique_angle_x/2,
                    oblique_angle_y*3 - i*oblique_angle_y);
                    offset_x++;
                }else{
                    mapNode.setPosition(-oblique_angle_x/2*3 + j*oblique_angle_x + symbol * i*oblique_angle_x/2,
                    oblique_angle_y*3 - i*oblique_angle_y);
                }   
                this._mapdata[i].push(0);     
            }
            offset_x = 0;            
        }

        this.node.y = 280;
        Main.AllData.MapDatas = this._mapdata;
    }

    /** 添加一个点亮的框子 */
    private addLighten (){

        for(let i=0;i<2;i++){
            let mapNode:cc.Node = new cc.Node();
            mapNode.name = "lighten";
            let spr:cc.SpriteFrame = cc.loader.getRes("main/76",cc.SpriteFrame);
            if(!spr){
                cc.warn("未获取到图片资源");
            }
            mapNode.setContentSize(spr.getOriginalSize().width,spr.getOriginalSize().height);
            let map_sr =  mapNode.addComponent(cc.Sprite);
            map_sr.spriteFrame = spr;
            map_sr.sizeMode = 1;         
            this.node.addChild(mapNode);
            mapNode.active = false;
            this._lightenList.push(mapNode);
        }
    }

    /**
     * 显示六边形悬置在蜂巢上面的点亮
     */
    public ShowLighten (node:Array<cc.Node>){

        if(node.length > 2){
            cc.log("对应六边形数量不对");
            return;
        }
        for(let i=0;i<node.length;i++){
            let lightpos = node[i].getPosition();
            if(this._lightenList[i]){
                this._lightenList[i].active = true;
                this._lightenList[i].setPosition(lightpos);
            }        
        }       
    }

    /** 隐藏显示的光标 */
    public HideLighten (){

        for(let i=0;i<this._lightenList.length;i++){
            
            if(this._lightenList[i].active){
                this._lightenList[i].active = false;
            }
        }       
    }

    /**
     * 播放消除动画
     * @param beginIndex 消除起始位置  已该位置为中心，其他向这个位置消除
     * @param eliminate 可以消除的位置
     * @param complete 消除完成回调
     */
    public EliminateAnimation (beginIndex:number,eliminate:Array<number>,complete:()=>void){

        if(beginIndex < 0 && eliminate.length == 0){
            cc.warn("位置不对");
            return;
        }

        //节点合成回调
        let completecallback = ()=>{
            
            Main.AllData.EliminateUpgrade(beginIndex,eliminate);

            let lev = Main.AllData.getLevel(beginIndex);
            let node = this.findmoveNode(eliminate[eliminate.indexOf(beginIndex)]);   
            Main.gameControl._Hexagonal.Hexagonal_lv(lev,node);

            let data:Array<number> = Main.AllData.ConvertNumberingTodata(beginIndex);
            let canEliminate = Main.AllData.CalculatesAll(data);

            if(complete) complete();
        }
        let beginPos = this._hexagonal_List[beginIndex].position;
        
        let completecall = Main.CountCallback(eliminate.length - 1,completecallback);
        for( let i=0;i<eliminate.length;i++){
            if(eliminate[i] != beginIndex){
                let moveNode = this.findmoveNode(eliminate[i]);
                if(moveNode){
                    
                    // let paren_pos = this._hexagonal_List[beginIndex].convertToWorldSpaceAR(cc.v2(0,0));
                    // let pos = moveNode.convertToNodeSpaceAR(paren_pos);
                    let moveact = cc.moveTo(1,beginPos);         
                    let callback =  cc.callFunc(()=>{
                        Main.gameControl._Hexagonal.MoveNodePool.put(moveNode);
                        moveNode.parent = null;
                        if(completecall) completecall();
                    })
                    moveNode.runAction(cc.sequence(moveact,callback));
                }
            }
        }

    }

    public findmoveNode(pos){

        let mapnodes = Main.gameControl.mapNode.children;
        for(let i=0;i<mapnodes.length;i++){
            let moveHexa:MoveHexa = mapnodes[i].getComponent(MoveHexa);
            if(moveHexa){
                if(moveHexa.locationPos == pos){
                    return mapnodes[i];
                }
            }
        }
        return null;
        
    }

    // update (dt) {}
}

